package com.ic.loan.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.*
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.ktx.immersionBar
import com.ic.loan.R
import com.ic.loan.activity.MainActivity
import com.ic.loan.activity.WhyChooseUsActivity
import com.ic.loan.bean.MyWallet
import com.ic.loan.http.ApiUrl
import com.ic.loan.http.HttpUtils
import com.ic.loan.http.ResultListener
import com.ic.loan.pop.RedEnvelopePop
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.impl.LoadingPopupView
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_home.view.*


class HomeFragment : Fragment() {
    private var myWallet: MyWallet? = null
    private var loadingView: LoadingPopupView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        immersionBar {
            statusBarDarkFont(true)
        }
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loadingView = XPopup.Builder(requireContext()).asLoading()
        ImmersionBar.setStatusBarView(this, spStatusBar)
        view.tvUpdate.setOnClickListener {
            updateApproval()
        }
        view.tvWithdrawCash.setOnClickListener {
            XPopup.Builder(requireContext()).asConfirm(
                "",
                "Submitted Amount:${myWallet?.personal_amount}Rs",
                StringUtils.getString(R.string.cancel),
                StringUtils.getString(R.string.yes), {
                    takeMoney()
                }, {}, false
            ).show()
        }
        updateApproval()
    }

    private fun takeMoney() {
        tvWithdrawCash.setBackgroundResource(R.drawable.shape_uncheck)
        tvWithdrawCash.isEnabled = false
        showLoading()
        HttpUtils.postOkGo(mutableMapOf(
            "amount" to (myWallet?.personal_amount ?: "")
        ), ApiUrl.POST_USER_WITHDRAWCASH, object : ResultListener() {
            override fun onSucceeded(json: String?) {
                updateApproval()
            }

            override fun onFailed(content: String?) {
                super.onFailed(content)
                tvWithdrawCash.setBackgroundResource(R.drawable.shape_login_btn_bg)
                tvWithdrawCash.isEnabled = true
            }

            override fun onLoadFinish() {
                super.onLoadFinish()
                hintLoading()
            }
        },true)
    }

    private fun updateApproval() {
        showLoading()
        HttpUtils.postOkGo(mutableMapOf("timestamp" to System.currentTimeMillis() / 1000),
            ApiUrl.POST_USER_MYWALLET, object : ResultListener() {
                override fun onSucceeded(json: String?) {

                    myWallet = GsonUtils.fromJson(json, MyWallet::class.java)
                    clContext.visibility = View.VISIBLE
                    tvAmount.text = "₹ ${myWallet?.app_amount}"
                    tvAccountBalance.text = "₹ ${myWallet?.personal_amount}"
                    tvAccountToBeRepaid.text = "₹ ${myWallet?.loan_amount}"
                    tvAmountBottom.text = myWallet?.app_amount.toString()
                    tvOrderNo.text = myWallet?.order_no
                    tvTerm.text = "${myWallet?.cycle} months"
                    tvRepayment.text = "₹ ${myWallet?.amount}"
                    tvTime.text = myWallet?.mtime
                    tvWithdrawCash.setBackgroundResource(R.drawable.shape_uncheck)
                    tvWithdrawCash.isEnabled = false

                    if ((myWallet?.personal_amount?.toDouble() ?: 0.0) > 0) {
                        tvWithdrawCash.setBackgroundResource(R.drawable.shape_login_btn_bg)
                        tvWithdrawCash.isEnabled = true
                    }else{
                        tvWithdrawCash.setBackgroundResource(R.drawable.shape_uncheck)
                        tvWithdrawCash.isEnabled = false
                    }

                    if ((myWallet?.withdraw?.toDouble() ?: 0.0) > 0 || (myWallet?.personal_amount?.toDouble() ?: 0.0) > 0) {
                        tvAccountBalance.text = "₹ ${myWallet?.personal_amount}"
                        ll2.setBackgroundResource(R.drawable.shape_successful_withdrawal_btn_bg)
                        tvStatus2.setTextColor(ColorUtils.getColor(R.color.color_f37e20))
                        ivStatus2.setImageResource(R.mipmap.ic_complete)
                        ivProgress.setImageResource(R.mipmap.ic_progress0)
                    } else {
                        ivProgress.setImageResource(R.mipmap.ic_progress2)
                        ivStatus2.setImageResource(R.mipmap.ic_complete_not)
                        tvStatus2.setTextColor(ColorUtils.getColor(android.R.color.white))
                        ll2.setBackgroundResource(R.drawable.shape_uncheck)
                    }

                    if (myWallet?.loan_amount?.toDouble() ?: 0.0 > 0) {
                        tvAccountToBeRepaid.text = "₹ ${myWallet?.loan_amount}"
                        ll3.setBackgroundResource(R.drawable.shape_successful_withdrawal_btn_bg)
                        tvStatus.setTextColor(ColorUtils.getColor(R.color.color_f37e20))
                        ivStatus.setImageResource(R.mipmap.ic_complete)
                        ivProgress.setImageResource(R.mipmap.ic_progress1)
                    }

                    if (myWallet?.is_withdraw == 1) {
                        clContext.visibility = View.VISIBLE
                        tvAmount.text = "₹ ${myWallet?.app_amount}"
                        tvAccountBalance.text = "₹ ${myWallet?.personal_amount}"
                        tvAccountToBeRepaid.text = "₹ ${myWallet?.loan_amount}"
                        tvAmountBottom.text = myWallet?.app_amount.toString()
                        tvOrderNo.text = myWallet?.order_no
                        tvTerm.text = "${myWallet?.cycle} months"
                        tvRepayment.text = "₹ ${myWallet?.amount}"
                        tvTime.text = myWallet?.mtime
                        tvWithdrawCash.setBackgroundResource(R.drawable.shape_uncheck)
                        tvWithdrawCash.isEnabled = false
                    }
                }

                override fun onLoadFinish() {
                    hintLoading()
                }
            })
    }

    companion object {
        @JvmStatic
        fun newInstance() = HomeFragment()
    }

    fun showLoading() {
        loadingView?.show()
    }

    fun hintLoading() {
        loadingView?.dismiss()
    }
}